package com.lli.mc.untils;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;

import javax.imageio.ImageIO;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageTree;
import org.apache.pdfbox.rendering.PDFRenderer;

public class Pdf2JpgUtil {
    public static void pdf2jpg(File file) throws Exception {

        PDDocument doc = PDDocument.load(file);
        PDPageTree pages = doc.getPages();
        PDFRenderer pdfRenderer = new PDFRenderer(doc);
        int pageCounter = 0;
        for (PDPage page : pages) {
            // 可选dpi
            BufferedImage bim = pdfRenderer.renderImageWithDPI(pageCounter++,
                    200);
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ImageIO.write(bim, "jpg", os);
            byte[] datas = os.toByteArray();
            InputStream is = new ByteArrayInputStream(datas);

        }

        doc.close();
    }

}
